package com.gylang.print.controller;

import com.gylang.api.core.domain.AjaxResult;
import com.gylang.api.enums.BusinessType;
import com.gylang.common.annotation.Log;
import com.gylang.common.core.controller.BaseController;
import com.gylang.common.core.page.TableDataInfo;
import com.gylang.common.utils.poi.ExcelUtil;
import com.gylang.print.bo.SysPrintAddBo;
import com.gylang.print.bo.SysPrintEditBo;
import com.gylang.print.bo.SysPrintQueryBo;
import com.gylang.print.service.ISysPrintService;
import com.gylang.print.vo.SysPrintVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 打印模板Controller
 *
 * @author ruoyi
 * @date 2021-06-16
 */
@Api(value = "打印模板控制器", tags = {"打印模板管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/print/print")
public class SysPrintController extends BaseController {

    private final ISysPrintService iSysPrintService;

    /**
     * 查询打印模板列表
     */
    @ApiOperation("查询打印模板列表")
    @PreAuthorize("@ss.hasPermi('print:print:list')")
    @GetMapping("/list")
    public TableDataInfo<SysPrintVo> list(@Validated SysPrintQueryBo bo) {
        return iSysPrintService.queryPageList(bo);
    }

    /**
     * 导出打印模板列表
     */
    @ApiOperation("导出打印模板列表")
    @PreAuthorize("@ss.hasPermi('print:print:export')")
    @Log(title = "打印模板", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult<SysPrintVo> export(@Validated SysPrintQueryBo bo) {
        List<SysPrintVo> list = iSysPrintService.queryList(bo);
        ExcelUtil<SysPrintVo> util = new ExcelUtil<SysPrintVo>(SysPrintVo.class);
        return util.exportExcel(list, "打印模板");
    }

    /**
     * 获取打印模板详细信息
     */
    @ApiOperation("获取打印模板详细信息")
    @PreAuthorize("@ss.hasPermi('print:print:query')")
    @GetMapping("/{id}")
    public AjaxResult<SysPrintVo> getInfo(@NotNull(message = "主键不能为空")
                                                  @PathVariable("id") Long id) {
        return AjaxResult.success(iSysPrintService.queryById(id));
    }

    /**
     * 新增打印模板
     */
    @ApiOperation("新增打印模板")
    @PreAuthorize("@ss.hasPermi('print:print:add')")
    @Log(title = "打印模板", businessType = BusinessType.INSERT)
    @PostMapping()
    public AjaxResult<Void> add(@Validated @RequestBody SysPrintAddBo bo) {
        return toAjax(iSysPrintService.insertByAddBo(bo) ? 1 : 0);
    }

    /**
     * 修改打印模板
     */
    @ApiOperation("修改打印模板")
    @PreAuthorize("@ss.hasPermi('print:print:edit')")
    @Log(title = "打印模板", businessType = BusinessType.UPDATE)
    @PutMapping()
    public AjaxResult<Void> edit(@Validated @RequestBody SysPrintEditBo bo) {
        return toAjax(iSysPrintService.updateByEditBo(bo) ? 1 : 0);
    }

    /**
     * 删除打印模板
     */
    @ApiOperation("删除打印模板")
    @PreAuthorize("@ss.hasPermi('print:print:remove')")
    @Log(title = "打印模板" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult<Void> remove(@NotEmpty(message = "主键不能为空")
                                       @PathVariable Long[] ids) {
        return toAjax(iSysPrintService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }
}
